﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;

namespace queue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MessageQueue queue = new MessageQueue(MessageProcess.queuename);

            // queue.BeginReceive();// 异步接收消息。接收之后就删除
            // 给接收结束加一个委托
            queue.ReceiveCompleted +=
                new ReceiveCompletedEventHandler(OnReceiveComleted);

            //开始接收
            queue.BeginReceive();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                MessageProcess mp = new MessageProcess();
                mp.testMessage(textBox1.Text);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string queuename = ".\\private$\\testQueue";//队列名称
            MessageQueue q = new MessageQueue(queuename);
            q.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            if (q.CanRead)
            {
                System.Messaging.Message m = q.Receive();

                ReciveBox.Text = m.Body.ToString();
            }
        }
        private void OnReceiveComleted(object source, ReceiveCompletedEventArgs eventargs)
        {
            MessageQueue MsgQue = (MessageQueue)source;            
            System.Messaging.Message Msg = MsgQue.EndReceive(eventargs.AsyncResult);
            Msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            MsgQue.BeginReceive();
            
            //ReciveBox.Text = Msg.Body.ToString();
            Speech mySpeech = new Speech();
            mySpeech.SpeechText("请，1号患者到3号诊室");
        }
    }
}